load("@fbsource//tools/build_defs:check_dependencies_test.bzl", "assert_dependencies_test", "audit_dependents_test", "check_dependencies_test", "check_mutually_exclusive_dependencies_test")

### This file contains some "e2e tests" for the BXL check_dependencies_test infra.

ALLOW_LIST = [
    ".*:useless_target.*",
    "fbsource//xplat/configurations/buck/apple/common_files:dummy.c",
    "fbsource//third-party/libgcc.*",
    "fbsource//third-party/tp2/libgcc.*",
]

BLOCK_LIST = [
    "//this_target_will_never_exist_at_least_we_hope.*",
]

TARGET1 = "fbcode//buck2/tests/targets/check_dependencies_test:useless_target_1"

TARGET2 = "fbcode//buck2/tests/targets/check_dependencies_test:useless_target_2"

TARGET3 = "fbcode//buck2/tests/targets/check_dependencies_test:useless_target_3"

CONTACTS = ["build_infra"]

oncall("build_infra")

check_dependencies_test(
    name = "allow_list_and_block_none",
    allowlist_patterns = ALLOW_LIST,
    contacts = CONTACTS,
    mode = "allowlist",
    target = TARGET2,
)

check_dependencies_test(
    name = "allow_none_and_block_list",
    blocklist_patterns = BLOCK_LIST,
    contacts = CONTACTS,
    mode = "blocklist",
    target = TARGET2,
)

check_dependencies_test(
    name = "allow_list_and_block_list",
    allowlist_patterns = ALLOW_LIST,
    blocklist_patterns = [
        ".*:useless_target_1*",
    ],
    contacts = CONTACTS,
    expect_failure_msg = "Found banned targets",
    mode = "allowlist",
    target = TARGET2,
)

check_dependencies_test(
    name = "allow_list_and_block_emptylist",
    allowlist_patterns = ALLOW_LIST,
    blocklist_patterns = [],
    contacts = CONTACTS,
    mode = "allowlist",
    target = TARGET2,
)

check_dependencies_test(
    name = "allow_emptylist_and_block_list",
    allowlist_patterns = [],
    blocklist_patterns = BLOCK_LIST,
    contacts = CONTACTS,
    mode = "blocklist",
    target = TARGET2,
)

check_dependencies_test(
    name = "fail_blocklist",
    blocklist_patterns = [".*:useless_target_1"],
    contacts = CONTACTS,
    expect_failure_msg = "Found blocklisted targets",
    mode = "blocklist",
    target = TARGET2,
)

check_dependencies_test(
    name = "fail_blocklist_with_allowlist",
    allowlist_patterns = [
        ".*:useless_target_1",
        ".*:useless_target_2",
    ],
    blocklist_patterns = [".*:useless_target.*"],
    contacts = CONTACTS,
    mode = "blocklist",
    target = TARGET2,
)

check_dependencies_test(
    name = "fail_allowlist",
    allowlist_patterns = [".*:nonexistent_target"],
    contacts = CONTACTS,
    expect_failure_msg = "Found banned targets",
    mode = "allowlist",
    target = TARGET2,
)

assert_dependencies_test(
    name = "expected_deps",
    contacts = CONTACTS,
    expected_deps = [
        TARGET1,
    ],
    target = TARGET2,
)

assert_dependencies_test(
    name = "fail_expected_deps",
    contacts = CONTACTS,
    expect_failure_msg = "Expected dependencies not found",
    expected_deps = [
        TARGET2,
    ],
    target = TARGET1,
)

audit_dependents_test(
    name = "audit_dependents_test",
    allowlist_patterns = [".*check_dependencies_test:useless_target_2"],
    contacts = CONTACTS,
    source_target = TARGET3,
    target = TARGET1,
)

audit_dependents_test(
    name = "fail_audit_dependents_test",
    allowlist_patterns = [".*check_dependencies_test:useless_target_[^2]"],
    contacts = CONTACTS,
    expect_failure_msg = "Disallowed rules were found",
    source_target = TARGET3,
    target = TARGET1,
)

# Tests for check_mutually_exclusive_dependencies_test

check_mutually_exclusive_dependencies_test(
    name = "mutually_exclusive_only_one_present",
    contacts = CONTACTS,
    mutually_exclusive_group = [
        ".*:useless_target_1",
        ".*:useless_target_999_nonexistent",
    ],
    target = TARGET2,
)

check_mutually_exclusive_dependencies_test(
    name = "mutually_exclusive_none_present",
    contacts = CONTACTS,
    mutually_exclusive_group = [
        ".*:useless_target_888_nonexistent",
        ".*:useless_target_999_nonexistent",
    ],
    target = TARGET2,
)

check_mutually_exclusive_dependencies_test(
    name = "mutually_exclusive_exact_targets_no_regex",
    contacts = CONTACTS,
    mutually_exclusive_group = [
        "fbcode//buck2/tests/targets/check_dependencies_test:useless_target_1",
        "fbcode//buck2/tests/targets/check_dependencies_test:useless_target_999_nonexistent",
    ],
    target = TARGET2,
)

check_mutually_exclusive_dependencies_test(
    name = "fail_mutually_exclusive_multiple_present",
    contacts = CONTACTS,
    expect_failure_msg = "Found .* mutually exclusive dependencies",
    mutually_exclusive_group = [
        ".*:useless_target_1",
        ".*:useless_target_2",
    ],
    target = TARGET3,
)
